package com.ymt.bpm.web.rest.platform.config;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PDict;
import com.ymt.bpm.service.platform.config.DictService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/9/20.
 */
@RestController
@RequestMapping("/api/v1/platform/config/dict")
public class DictResource {

    /**
     * Logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * DictService
     */
    @Autowired
    private DictService ds;

    @GetMapping("/query")
    public JSONObject query(HttpServletRequest req,
                              @RequestParam(name="appId", required = false)String appId,
                              @RequestParam(name="search", required = false)String search,
                              @RequestParam(name="sort", defaultValue = "TYPE")String sort,
                              @RequestParam(name="order", defaultValue = "ASC")String order,
                              @RequestParam("offset")int offset,
                              @RequestParam("limit")int limit,
                              @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (search!=null && search.length()>0) {
            search = EncodeUtil.decode(search);
        }
        if (log.isDebugEnabled()) {
            log.debug("query tenantId=" + tenantId);
            log.debug("query appId=" + appId);
            log.debug("query search=" + search);
            log.debug("query sort=" + sort);
            log.debug("query order=" + order);
            log.debug("query offset=" + offset);
            log.debug("query limit=" + limit);
            log.debug("query timezoneOffset=" + timezoneOffset);
        }
        return ds.query(tenantId, appId, search, sort, order, offset, limit, timezoneOffset);
    }

    @PostMapping("/save")
    public JSONObject save(HttpServletRequest req, PDict dict) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("save tenantId=" + tenantId);
            log.debug("save dict.id=" + dict.getId());
            log.debug("save dict.type=" + dict.getType());
        }
        return ds.save(tenantId, loginName, displayName, dict);
    }

    @PostMapping(value="/checkType", produces = "plain/text")
    public String checkBoname(HttpServletRequest req,
                              @RequestParam(value="id", required=false)Long id,
                              @RequestParam("type")String type) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkPosName tenantId=" + tenantId);
            log.debug("checkPosName id=" + id);
            log.debug("checkPosName type=" + type);
        }
        return ds.checkAttr(tenantId, "type", type, id);
    }

    @DeleteMapping("/{id}")
    public JSONObject delete(HttpServletRequest req,
                             @PathVariable(value="id", required=false)Long id) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete id=" + id);
        }
        return ds.delete(tenantId, loginName, displayName, id);
    }

    @GetMapping("/value/query")
    public JSONObject queryValue(HttpServletRequest req,
                            @RequestParam(name="id")Long id) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("queryValue tenantId=" + tenantId);
            log.debug("queryValue id=" + id);
        }
        return ds.queryValue(tenantId, id);
    }

    @PostMapping("/value/save")
    public JSONObject saveValue(HttpServletRequest req,
                                 @RequestParam(name="id")Long id,
                                 @RequestParam(name="type")String type,
                                 @RequestParam(name="typedesc")String typedesc,
                                 @RequestParam(name="relationkey")String relationkey,
                                 @RequestParam(name="zh")String zh,
                                 @RequestParam(name="en")String en,
                                 @RequestParam(name="sort")Short sort
                                 ) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("saveValue tenantId=" + tenantId);
            log.debug("saveValue id=" + id);
            log.debug("saveValue type=" + type);
            log.debug("saveValue typedesc=" + typedesc);
            log.debug("saveValue relationkey=" + relationkey);
            log.debug("saveValue zh=" + zh);
            log.debug("saveValue en=" + en);
        }
        return ds.saveValue(tenantId, loginName, displayName, id, type, typedesc, relationkey, zh, en, sort);
    }

    @PostMapping("/value/check")
    public String checkValue(HttpServletRequest req,
                                @RequestParam(name="id")Long id,
                                @RequestParam(name="type")String type,
                                @RequestParam(name="relationkey")String relationkey
    ) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkValue tenantId=" + tenantId);
            log.debug("checkValue id=" + id);
            log.debug("checkValue type=" + type);
            log.debug("checkValue relationkey=" + relationkey);
        }
        return ds.checkValue(tenantId, relationkey, type, id);
    }

    @DeleteMapping("/value/delete")
    public JSONObject delValue(HttpServletRequest req,
                             @RequestParam(name="id")Long id,
                             @RequestParam(name="type")String type,
                             @RequestParam(name="relationkey")String relationkey,
                             @RequestParam(name="sort")Short sort
    ) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delValue tenantId=" + tenantId);
            log.debug("delValue id=" + id);
            log.debug("delValue type=" + type);
            log.debug("delValue relationkey=" + relationkey);
            log.debug("delValue sort=" + sort);
        }
        return ds.delValue(tenantId, loginName, displayName, relationkey, sort, type, id);
    }

    @PostMapping("/value/reorder")
    public JSONObject reorderValue(HttpServletRequest req,
                               @RequestParam(name="id")Long id,
                               @RequestParam(name="type")String type,
                               @RequestParam(name="sorts")String sorts
    ) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("reorderValue tenantId=" + tenantId);
            log.debug("reorderValue id=" + id);
            log.debug("reorderValue type=" + type);
            log.debug("reorderValue sorts=" + sorts);
        }
        return ds.reorderValue(tenantId, loginName, displayName, sorts, type, id);
    }
    
    @PostMapping("queryDictDhx")
    public JSONObject queryDictDhx(HttpServletRequest req,
            @RequestParam(name="appId", required = false)String appId,
            @RequestParam("args")String args,
            @RequestParam("ordercolumns")String ordercolumns,
            @RequestParam("fromRow")int fromRow,
            @RequestParam("toRow")int toRow,
            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("queryDictDhx tenantId=" + tenantId);
            log.debug("queryDictDhx appId=" + appId);
            log.debug("queryDictDhx args=" + args);
            log.debug("queryDictDhx ordercolumns=" + ordercolumns);
            log.debug("queryDictDhx fromRow=" + fromRow);
            log.debug("queryDictDhx toRow=" + toRow);
            log.debug("queryDictDhx timezoneOffset=" + timezoneOffset);
        }
        return ds.queryDictDhx(tenantId, appId, args, ordercolumns, lang, fromRow, toRow, timezoneOffset);
    }

}
